package com.bigshen.goods.goodsserviceprovider.feign;

import com.bigshen.goods.goodsserviceprovider.converter.ItemConverter;
import com.bigshen.goods.goodsserviceprovider.domain.ItemStockDo;
import com.bigshen.goods.goodsserviceprovider.mapper.entitys.TbItem;
import com.bigshen.goods.goodsserviceprovider.service.IItemService;
import com.bigshen.springcloud.api.R;
import com.bigshen.springcloud.clients.IGoodsFeignClient;
import com.bigshen.springcloud.dto.ItemStockDto;
import com.bigshen.springcloud.vo.ItemDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class GoodsFeignClient implements IGoodsFeignClient {

    @Autowired
    IItemService iItemService;

    @Autowired
    ItemConverter itemConverter;

    @Override
    public R decreaseStock(List<ItemStockDto> itemStockDtoList) {
        log.info("begin GoodsFeignClient.decreaseStock: " + itemStockDtoList);
        List<ItemStockDo> itemStockDoList = itemConverter.itemStockDo2DoList(itemStockDtoList);
        boolean rs = iItemService.decreaseStock(itemStockDoList);
        return rs ? new R.Builder<>().buildOk() : new R.Builder<>().buildFail();
    }

    @Override
    public R<List<ItemDetailVo>> getItemsByIds(List<Long> ids) {
        log.info("begin GoodsFeignClient.getItemsByIds: " + ids);
        List<TbItem> tbItems = iItemService.findItemsByIds(ids);
        List<ItemDetailVo> itemDetailVoList = itemConverter.itemDetail2VoList(tbItems);
        return new R.Builder<List<ItemDetailVo>>().setData(itemDetailVoList).buildOk();
    }

}
